"""
a really poor popup box.

smart enough to display text and perhaps images
in a grid like or column like fashion.

   image  |  text about image
          |  can span multiple
          |  lines.

clicking a mouse button will exit."""

import pygame
from pygame.locals import *
import getpass

import data
import constants as c
from albow.resource import get_font, get_image

def game_complete(screen, scorer):
    'show the finishing game stats and high scores.'
    img, rect = data.load_image('gamecomplete.png', None)
    screen.blit(img, (0, 0))
    pygame.display.flip()

    normaltext = get_font(22, "Vera.ttf")
    boldtext = get_font(22, "VeraBd.ttf")

    # show the user his stats.

    def print_score(screen, y, texttype, rank, name, saved, total,
                    arrow=False, color=None):
        if not color: color = c.GREY
        if arrow: rendertxt(screen, "you: ", texttype, 270, y, color=color)

        for txt, x in (("%02d" % int(rank), 340),
                       ("%s" % name, 418),
                       ("%03d" % int(saved), 531),
                       ("%03d" % int(total), 680)):
            ynew = rendertxt(screen, txt, texttype, x, y, color=color)
        return ynew

    myrecord = [[getpass.getuser(), scorer.saved, scorer.total_balls]]
    y = 320
    for rank, record in enumerate(data.highscores(myrecord)):
        if not record: continue
        if rank > 2: break
        name, saved, total = record
        y = print_score(screen, y - 20, normaltext,
                           rank + 1, name, int(saved), int(total))

    y = print_score(screen, y - 20, boldtext,
                        data.highscores.lastrank + 1, myrecord[0][0],
                        myrecord[0][1],
                        myrecord[0][2],
                        arrow=True, color=c.GREY)
     
    pygame.display.update()


    wait_for_interaction()

def popup(display, config):
    'a full screen instructions before play popup'

    instructions = get_image("instructions.png")

    display.blit(instructions, (0, 0))
    pygame.display.flip()

    clk = pygame.time.Clock()
    running = True
    while running:
        clk.tick(c.FPS)

        for e in pygame.event.get():
            if e.type == MOUSEBUTTONDOWN:
                running = False
            if e.type == QUIT:
                raise SystemExit("Poweroff")

def rendertxt(display, text, font, x, y, color=None):
    if not color: color = c.WHITE
    BIG_BUFFER = 30
    SMALL_BUFFER = 5
    lines = data.wrap_multi_line(text, font, 300)
    for line in lines:
        surf = font.render(line, True, color)
        display.blit(surf, (x, y))
        y += SMALL_BUFFER + surf.get_height()
    return y + BIG_BUFFER

def wait_for_interaction():
    clk = pygame.time.Clock()
    running = True
    pygame.time.delay(500)
    pygame.event.clear()
    while running:
        clk.tick(c.FPS)

        for e in pygame.event.get():
            if e.type == MOUSEBUTTONDOWN:
                running = False
            if e.type == QUIT:
                raise SystemExit("Poweroff")


